<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of category_model
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Admin_specility_model extends CI_Model {

    //put your code here


    function __construct() {
        parent::__construct();
        log_message('debug', 'Init Model Initialized');
    }

  function get_specilities($order_type=NULL, $num=NULL, $offset=NULL){
   $subcond = " where status=51";
   $limit = '';       
      if ($num > 0)
            $limit = ' limit ' . $offset . ',' . $num;
			
  $sql="select * from specialities" .$subcond. ' order by "specilities" ' . $order_type . $limit;
  $query=$this->db->query($sql);
  $result=$query->result();
  return $result;
  }
  function get_specilities_by_id($id){
  
  $sql="select * from specialities where id=$id;";
  $query=$this->db->query($sql);
  $result=$query->row();
  return $result;
  }
    public function category_list_front() {
        
    }

    public function category_update($id=NULL, $detail=NULL) {
        $data = array('category' => $detail);
        $this->db->where('id', $id);
        $this->db->update('keyword_category', $data);
    }

    public function category_delete($id=NULL) {
	
		
	// --- generate listing id list-------			
		$sql="select * from listing_master where category=$id ";
		$query=$this->db->query($sql);
		$result=$query->result();
		     if ($query->num_rows() > 0) {
                $key = '';
                foreach ($result as $listing):
                    $key .= $listing->id . ',';
					
					//-- delete banner---
					if($listing->banner!='nobanner.gif')
					{
					$banner=BASEPATH.'../upload/banner/'.$listing->banner;
					unlink($banner);
					}
					
					//-- delete logo
					if($listing->logo!='nologo.gif')
					{
					$logo=BASEPATH.'../upload/logos/'.$listing->logo;
					unlink($logo);
					}
                endforeach;
                $length = strlen($key);
                $key = substr_replace($key, '', $length - 1, $length);
				
				}
				if(isset($key))
                {
		// --- delete listing_keyword-------	
		$sql="delete from listing_keyword where listing_id in($key)";
		$this->db->query($sql);
		
		// --- delete review_master-------	
		$sql="delete from review_master where listing_id in($key)";
		$this->db->query($sql);
		
		// --- delete report_incorrect_master-------	
		$sql="delete from report_incorrect_master where listing_id in($key)";
		$this->db->query($sql);
		
		// --- delete report_reclaim_master-------	
		$sql="delete from report_reclaim_master where listing_id in($key)";
		$this->db->query($sql);
		
		// --- delete enquiry master-------	
		$sql="delete from enquiry_master where listing_id in($key)";
		$this->db->query($sql);
		
		// --- delete photos-------	
		$sql="select image from photos where listing_id in($key)";
		$query=$this->db->query($sql);
		$result=$query->result();
		foreach($result as $photos):
		$photo=BASEPATH.'../upload/photos/'.$photos->image;
		unlink($photo);
		endforeach;
		}
		// --- delete listing_master-------	
		$this->db->where('category', $id);
        $this->db->delete('listing_master');
		
		// --- delete keyword_master-------
        $this->db->where('code', $id);
		$this->db->delete('keyword_master');
		
		// --- delete keyword_category-------	
		$this->db->where('id', $id);
        $this->db->delete('keyword_category');
    }

    public function category_insert($detail=NULL) {
        $data = array('category' => $detail);
        $this->db->insert('keyword_category', $data);
    }

    function get_category($id=NULL) {
        $sql="select * from category where id=$id";
		$query=$this->db->query($sql);
        $row = $query->row();
        if (!$row) {
            return 'No Category';
        } else {
            return $row;
        }
    }

    function category_list($num=NULL, $offset=NULL, $category=NULL) {
        $this->db->select('*')->from('keyword_category');
        $this->db->where('status', '51');
        $this->db->like('category', $category);
        $this->db->order_by('category', 'ASC');
        $this->db->like('category', $category);
        $this->db->limit($num, $offset);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    public function subcategory_list($listing_id=NULL) {
        $sql = "select listing_key_id from listing_keyword where listing_id='$listing_id'";
        $query = $this->db->query($sql);
        $result = $query->result();
        if ($query->num_rows() > 0) {
            $key = '';
            foreach ($result as $keyword_id):
                $key .= $keyword_id->listing_key_id . ',';
            endforeach;
            $length = strlen($key);
            $key = substr_replace($key, '', $length - 1, $length);
        }
        $sql = "SELECT * FROM `keyword_master` WHERE id in($key)";
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

}

?>